//
//  CYYaoQingHuiDaViewController.m
//  cyydt
//
//  Created by chu on 2019/2/3.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYYaoQingHuiDaViewController.h"
#import "CYYaoQingHuiDaTuiJianViewController.h"
#import "CYYaoQingHuiDaHaoYouViewController.h"

@interface CYYaoQingHuiDaViewController ()<SGTopScrollMenuDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) SGTopScrollMenu *topMenu;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UIView *titleView;

@end

@implementation CYYaoQingHuiDaViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self setChildViewControllers];
    
    self.navigationItem.titleView = self.titleView;
    
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    // 1 计算滚动的位置
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.mainScrollView.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    [self.topMenu selectLabel:self.topMenu.allTitleLabel[index]];
}

#pragma mark - SGTopScrollMenuDelegate
- (void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    [self showVc:index];
    
}

- (void)setChildViewControllers{
    CYYaoQingHuiDaTuiJianViewController *commend = [[CYYaoQingHuiDaTuiJianViewController alloc] init];
    commend.questionId = self.questionId;
    commend.cateId = self.cateId;
    commend.model = self.model;
    commend.view.frame = CGRectMake(0, 0, kWindowW, self.mainScrollView.frame.size.height);
    [self.mainScrollView addSubview:commend.view];
    [self addChildViewController:commend];
    
    CYYaoQingHuiDaHaoYouViewController *info = [[CYYaoQingHuiDaHaoYouViewController alloc] init];
    info.questionId = self.questionId;
    info.model = self.model;
    [self addChildViewController:info];
    
}

- (UIView *)titleView{
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 170 , 44)];
        _titleView.center = CGPointMake(kWindowW / 2, 22);
        [_titleView addSubview:self.topMenu];
    }
    return _titleView;
}

- (SGTopScrollMenu *)topMenu{
    if (!_topMenu) {
        _topMenu = [SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 0, 170 , 44) NormalColor:RGBOF(0x999999) SelColor:RGBOF(0xF43636)];
        _topMenu.topScrollMenuDelegate = self;
        _topMenu.titlesArr = @[@"推荐", @"好友"];
    }
    return _topMenu;
}

@end
